part of YFree;

extension String_ on String {
  String yLocale() {
    final localization = YConfig.localizations[this];
    Locale? key;
    final s = YLocaleUtils.appLocale;
    final curLocale = (YLocaleUtils.appLocale ?? YLocaleUtils.appLocale_system!).languageCode;
    localization?.keys.forEach((key_for) {
      if (key_for.languageCode == curLocale) {
        key = key_for;
        return;
      }
    });
    return localization != null && key != null ? localization[key]! : "(UnLocaled_$this)";
  }
}

class YLocaleUtils {
  static Locale? appLocale, appLocale_system;

  // null：自动
  static void updateAppLocale(Locale? locale) {
    if (appLocale == locale) return;
    appLocale = locale;
    if (locale == null) {
      yDelete(YConfig.localizationKey);
    } else {
      ySave(YConfig.localizationKey, appLocale!.languageCode);
    }
    YConfig.onLocaleLoad.call(appLocale ?? appLocale_system!);
    for (var yPage in yPages) {
      if (yPage.mounted && yPage.yIsAlive) yPage.setState(() {});
    }
  }
}
